var RoleResourceManaged = function() {
	return {
		/**
		 * 初始状态
		 */
		init : function() {
			var doClick = function() {
				//ResourcesManager.reSearch();
//	 			alert("");
		   	};
			UITree.initTree({
			   	"url" 		: ctx+"/resource/getTreeResources?now="+new Date().getTime(),
			   	"id"	 	: "tree_2",
			   	"checkbox"	:true,
			   	"click"		: doClick,
			   	"disable"	:false
		   	});
			var roleId=$("#roleId").val();
			$.ajax({
				"url" : ctx + "/roleResource/doSearch/"+roleId+"?now="+new Date().getTime(),
				"cache" : false,
				"success" : function(data) {
					if(data.type=='success'){
							var selecteds=data.data;	
							if(selecteds&&selecteds.length>0){
								var ids="";
								for(var i=0,len=selecteds.length;i<len;i++){
									var _resourceId = selecteds[i].resourceId;
									if(ids==""){
										ids=_resourceId;
									}else{
										ids=ids+","+_resourceId;
									}
								}
								var param={'treeId':'tree_2','ids':ids};
								UITree.setSelected(param);
							}
					}else{
						BootstrapDialog.show({
							type : BootstrapDialog.TYPE_WARNING,
							title : messages.public_cue_warning,
							message : data.message,
							buttons : [ {
								label : messages.public_cue_sure,
								cssClass : 'btn btn-sm btn-primary',
								action : function(dialogItself) {
									dialogItself.close();
								}
							} ]
						});
					}
				}
			});
			//ResourcesManager.init();
			this.initListener();
		},

		/**
		 * 监听开关事件
		 */
		initListener : function() {
			var roleId=$("#roleId").val();
			$("#addBtn").click(function(){
				var ids=UITree.getSelected("tree_2");
				var ids2=UITree.getUndetermined("tree_2");
				if(ids2&&ids2.length>0){
					for(var i=0,len=ids2.length;i<len;i++){
						ids.push(ids2[i]);
					}
				}
				var postData={'roleId':roleId,'resources':ids};
				$.ajax({
					"dataType": "json",
	    			"contentType":"application/json",
	    			"cache" : false,
					"type" : "POST",
					"data" : JSON.stringify(postData),
					"url" : ctx + "/roleResource/edit?now="+new Date().getTime(),
					"success" : function(data) {
						if(data.type==='success'){
							BootstrapDialog.show({
								type : BootstrapDialog.TYPE_WARNING,
								title : messages.public_cue_warning,
								message : messages.public_cue_saveOk,
								buttons : [ {
									label : messages.public_cue_sure,
									cssClass : 'btn btn-sm btn-primary',
									action : function(dialogItself) {
										dialogItself.close();
										window.location.href=ctx+'/role/list.html'; 
									}
								} ]
							});
							
						}else{
							BootstrapDialog.show({
								type : BootstrapDialog.TYPE_WARNING,
								title : messages.public_cue_warning,
								message : data.message,
								buttons : [ {
									label : messages.public_cue_sure,
									cssClass : 'btn btn-sm btn-primary',
									action : function(dialogItself) {
										dialogItself.close();
									}
								} ]
							});
						}
					}
				});
			});
			$("#cancelBtn").click(function(){
				window.location.href=ctx+'/role/list.html'; 
			});
		}
	};

}();